﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;


namespace ZHDJ.BranchCenter.Module.RelationshipTransfer
{
    public partial class OrgTransferList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        #region 数据加载
        private void LoadData()
        {
            // 权限检查
            //CheckPowerWithButton("PhotoWallAdd", btnAdd);

            ppBranch.Value = this.BranchID;
            ppBranch.Text = ControlBusiness.GetBranchPickerText(this.BranchID);

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;

            PageContext.RegisterStartupScript(Window1.GetShowReference() + Window1.GetMaximizeReference() + Window1.GetHideReference());
            BindGrid();
        }

        private void BindGrid()
        {
            var ds = PartyRelationshipTransfer.GetBranchGridData(Grid1.PageIndex, Grid1.PageSize, ppBranch.Value);

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("PhotoWallEdit", Grid1, "editField");
            //CheckPowerWithLinkButtonField("PhotoWallDelete", Grid1, "deleteField");
        }

        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
            DataRowView row = e.DataItem as DataRowView;
            var btnHandler1 = Grid1.FindColumn("btnHandler") as LinkButtonField;

            if (btnHandler1 != null)
            {
                if (row["Status"].ToString() == "审批中" && row["ToUser"].ToString() == this.BranchID)
                {
                    this.Window1.Title = Convert.ToString(row["Title"]);

                    var url = Convert.ToString(row["TaskUrl"]);
                    if (!url.Contains("TaskID="))
                    {
                        if (url.Contains("?"))
                        {
                            url += "&TaskID=" + row["ID"];
                        }
                        else
                        {
                            url += "?TaskID=" + row["ID"];
                        }
                    }
                    btnHandler1.OnClientClick = this.Window1.GetShowReference(url, Convert.ToString(row["Title"]));
                    btnHandler1.Enabled = true;
                }
                else
                {
                    btnHandler1.Enabled = false;
                }
            }
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                //if (!CheckPower("PhotoWallDelete"))
                //{
                //    CheckPowerFailWithAlert();
                //    return;
                //}

                var del = DB.PartyRelationshipTransfers.FirstOrDefault(t => t.ID == id);
                DB.PartyRelationshipTransfers.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }

        #endregion

        #region 按钮事件

        protected void ppBranch_TextChanged(object sender, EventArgs e)
        {
            ppBranch.ShowTrigger1 = !string.IsNullOrEmpty(ppBranch.Text);
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            //if (!CheckPower("PhotoWallDelete"))
            //{
            //    CheckPowerFailWithAlert();
            //    return;
            //}

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyRelationshipTransfers.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyRelationshipTransfers.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}